package com.hxgis.oc.util.w3d.base;

import cn.hutool.json.JSONObject;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Setter
@Getter
@Component
public class W3dConvertMeta {
    public static final String VarName = "meta2w3dm";

    private float XMin;
    private float YMin;
    private float XMax;
    private float YMax;
    private float ClampMin = 0;
    private float ClampMax = 100;
    private int Width;
    private int Height;
    private int Depth = 1;
    private float FixValue = 0.0f;
    private String DataType = "Float";

    private File fileMeta;
    private File fileBin;
    private File fileBin2;

    @Getter
    List<byte[]> varBufferList = new ArrayList<>();

    public void clean()
    {
        try {
            if(fileMeta != null)
                fileMeta.delete();
            if(fileBin != null)
                fileBin.delete();
            if(fileBin2 != null)
                fileBin2.delete();
        } catch(Exception ignored){}
    }

    public void setExtent(float xMin,float yMin,float xMax,float yMax)
    {
        XMin = xMin;
        YMin = yMin;
        XMax = xMax;
        YMax = yMax;
    }

    public void setClampMinMax(float clampMin,float clampMax)
    {
        ClampMin = clampMin;
        ClampMax = clampMax;
    }

    public File generateMeta(String w3dmFileName) {
        byte[] b1 = varBufferList.get(0);
        byte[] b2 = varBufferList.size() > 1 ? varBufferList.get(1) : null;
        return generateMeta(b1,b2,w3dmFileName);
    }

    public File generateMeta(byte[] values1,byte[] values2,String w3dmFileName){
        log.debug("转化w3dm: {}",w3dmFileName);
        JSONObject json = new JSONObject();
        json.set("XMin",XMin);
        json.set("YMin",YMin);
        json.set("XMax",XMax);
        json.set("YMax",YMax);
        json.set("Width",Width);
        json.set("Height",Height);
        json.set("Depth",Depth);
        json.set("FixValue",FixValue);

        json.set("ClampMin",ClampMin);
        json.set("ClampMax",ClampMax);
        json.set("DataType",DataType);
        json.set("BigEndian",true);
        json.set("FlipX",true);
        json.set("FlipY",false);

        fileMeta = new File(w3dmFileName + ".meta");
        fileBin = new File(w3dmFileName + ".bin");

        try {
            if(values2 != null)
            {
                json.set("BinNum",2);
                fileBin2 = new File(w3dmFileName + ".bin2");
                FileOutputStream fos3 = new FileOutputStream(fileBin2);
                fos3.write(values2);
                fos3.close();
            }

            FileOutputStream fos1 = new FileOutputStream(fileMeta);
            fos1.write(json.toString().getBytes("UTF-8"));
            fos1.close();

            FileOutputStream fos2 = new FileOutputStream(fileBin);
            fos2.write(values1);
            fos2.close();
        } catch (IOException e) {
            log.error(e.getMessage());
        }

        return fileMeta;
    }
}
